import os
import threadpool

from DataCastleMatch.tiancheng.base.base_helper import *
print(features_base_path)
tst_merge = pd.read_pickle(features_base_path+"tst_merge.pkl")

label = tst_merge[tag_header]#57615
UID_LIST = list(set(tst_merge[tag_hd.UID].tolist()))
print(len(UID_LIST))  #660643
ftr_path = features_base_path+"tst_merge_ftr.pkl"
ftr_list = []
if os.path.exists(ftr_path):
    ftr_list = pd.read_pickle(ftr_path)
    [UID_LIST.remove(x[0]) for x in ftr_list]
# 异常点检测

def extract_feature(n, m):
    try:
        for i,uid in enumerate(UID_LIST[n:m]):
            print(i,uid)
            uid_data = tst_merge[tst_merge[op_hd.UID] == uid]
            df, outliers_rate, tag_uid = outliers_detection(uid_data)
            ftr = get_feature(df, tag_uid)
            ftr.append(outliers_rate)
            ftr_list.append(ftr)
    except:
        traceback.print_exc()
        temp_path = features_base_path + "tst_merge_ftr_{}.pkl".format(k)
        pd.to_pickle(ftr_list, temp_path)
# value0 = [[row for row in ftr_list] for i in range(len(ftr_list[0]))]

clos_time = [(0, 6000), (6000, 12000), (12000, 18000), (18000,24000), (24000, 30000), (30000, 36000), (36000, 42000),
             (42000, 48000), (48000, 54000), (54000, 60000)]

pool = threadpool.ThreadPool(10)
requests = threadpool.makeRequests(extract_feature, clos_time)
[pool.putRequest(req) for req in requests]
pool.wait()

pd.to_pickle(ftr_list, ftr_path)
print(123)